<?php

/**
 * This is the model class for table "zy_iptvprogram_list".
 *
 * The followings are the available columns in table 'zy_iptvprogram_list':
 * @property integer $program_id
 * @property string $title
 * @property integer $year
 * @property integer $type_id
 * @property string $category
 * @property integer $country
 * @property integer $language
 * @property string $package_id
 * @property string $actor
 * @property integer $duration
 * @property integer $classification
 * @property integer $score_rating
 * @property string $video_link
 * @property integer $is_have_list
 * @property string $video_info
 * @property string $pic_link_large
 * @property string $pic_link_small
 * @property integer $play_count
 * @property integer $is_delete
 * @property string $remark
 * @property string $create_user
 * @property string $create_time
 * @property string $update_user
 * @property string $update_time
 * @property integer $latest
 * @property integer $popular
 * @property integer $recommend
 */
class IptvprogramList extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_iptvprogram_list';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		    array('title','required'),
			array('year, type_id, country, language, duration, classification, score_rating, is_have_list, play_count, is_delete, latest, popular, recommend', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>300),
			array('category, package_id, create_user, update_user', 'length', 'max'=>255),
			array('actor', 'length', 'max'=>50),
			array('video_link, video_info, pic_link_large, pic_link_small, remark', 'length', 'max'=>500),
			array('create_time, update_time', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('program_id, title, year, type_id, category, country, language, package_id, actor, duration, classification, score_rating, video_link, is_have_list, video_info, pic_link_large, pic_link_small, play_count, is_delete, remark, create_user, create_time, update_user, update_time, latest, popular, recommend', 'safe', 'on'=>'search'),
			array('pic_link_small', 'file',
			      'allowEmpty'=>true,
			      'types'=>'jpg, gif, png',
			      'maxSize'=>1024 * 1024 * 1, 
			      'tooLarge'=>'文件最大不超过1MB，请重新上传文件',
			      ),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		
		//'type'=>array(self::BELONGS_TO, 'IptvType', 'type_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'program_id' => Yii::t('base','Program Id'),
			'title' => Yii::t('base','Title'),
			'year' => Yii::t('base','Year'),
			'type_id' => Yii::t('base','Tv Type'),
			'category' => Yii::t('base','Tv Category'),
			'country' => Yii::t('base','Country'),
			'language' => Yii::t('base','Language'),
			'package_id' => Yii::t('base','PackageId'),
			'actor' => Yii::t('base','Actor'),
			'duration' => Yii::t('base','Duration'),
			'classification' => Yii::t('base','Classification'),
			'score_rating' => Yii::t('base','Score Rating'),
			'video_link' => Yii::t('base','Video Link'),
			'is_have_list' => Yii::t('base','Is Have List'),
			'video_info' => Yii::t('base','Video Info'),
			'pic_link_large' => Yii::t('base','Pic Link Large'),
			'pic_link_small' => Yii::t('base','Pic Link Small'),
			'play_count' => Yii::t('base','Play Count'),
			'is_delete' => Yii::t('base','Is Delete'),
			'remark' => Yii::t('base','Remark'),
			'create_user' => Yii::t('base','Create User'),
			'create_time' => Yii::t('base','Create Time'),
			'update_user' => Yii::t('base','Update User'),
			'update_time' => Yii::t('base','Update Time'),
			'latest' => Yii::t('base','Latest'),
			'popular' => Yii::t('base','Popular'),
			'recommend' => Yii::t('base','Recommend'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		
		
		$criteria->compare('program_id',$this->program_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('year',$this->year);
		$criteria->compare('type_id',$this->type_id);
		$criteria->compare('category',$this->category,true);
		$criteria->compare('country',$this->country);
		$criteria->compare('language',$this->language);
		$criteria->compare('package_id',$this->package_id,true);
		$criteria->compare('actor',$this->actor,true);
		$criteria->compare('duration',$this->duration);
		$criteria->compare('classification',$this->classification);
		$criteria->compare('score_rating',$this->score_rating);
		$criteria->compare('video_link',$this->video_link,true);
		$criteria->compare('is_have_list',$this->is_have_list);
		$criteria->compare('video_info',$this->video_info,true);
		$criteria->compare('pic_link_large',$this->pic_link_large,true);
		$criteria->compare('pic_link_small',$this->pic_link_small,true);
		$criteria->compare('play_count',$this->play_count);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_user',$this->create_user,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_user',$this->update_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('latest',$this->latest);
		$criteria->compare('popular',$this->popular);
		$criteria->compare('recommend',$this->recommend);
		//$criteria->with=array('type');
        $criteria->order = 'latest DESC,popular DESC,recommend DESC' ;//排序条件  
     
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return IptvprogramList the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
/**
*获取节目类型
*/
	public function getType()
	{
		$sql = "select * from zy_iptv_type";
		$results  = Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($results,'type_id','type_name');
	}
	/*
	 * 获取节目类型名称
	 */
	public function getTypeName($typeid){
		$sql = "select * from zy_iptv_type where type_id=".$typeid;
		$results  = Yii::app()->db->createCommand($sql)->queryAll();
		return $results[0]['type_name'];
	}
	/*
	*获取显示树形的节目类型	
	*/

    public function AllType()
	{
	    $sql='select type_id,type_name from zy_iptv_type';
	    $rs=yii::app()->db->createCommand($sql)->queryAll();
	    $a=array();
        for ($i = 0; $i < count($rs); $i ++) {
            array_push(
            $a,array(
            'text' => '<a href="'.yii::app()->createUrl('/IptvprogramList/admin',array('type_id'=>$rs[$i]['type_id'])).'">'.$rs[$i]['type_name'].'</a>'
                    ));   
        }  
    	return $a;
	}
	
	/*
	* 获取设置状态
	*/
	public function getLatestStatus($latest)
	{
	   if($latest==0){
	      return " ";
	   }elseif($latest==1){
	      return Yii::t('base','Latest');
	   }
	
	
	}
	
	public function getRecommendStatus($recommend)
	{
	   if($recommend==0){
	      return " ";
	   }elseif($recommend==1){
	      return Yii::t('base','Recommend');
	   }
	
	
	}
	
	public function getPopularStatus($popular)
	{
	   if($popular==0){
	      return " ";
	   }elseif($popular==1){
	      return Yii::t('base','Popular');
	   }
	
	
	}
	
	
	
	
	
}
